<?php

namespace frontend\controllers;

use Yii;
use backend\models\Product;
use frontend\models\ProductSearcher;
//use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\models\Category;
use frontend\models\RecentView;
use yii\data\Pagination;

/**
 * ProductController implements the CRUD actions for Product model.
 */
//class ProductController extends Controller
class ProductController extends SuperController {

	/**
	 * @inheritdoc
	 */
	public function behaviors() {
		return [
			'verbs' => [
				'class' => VerbFilter::className(),
				'actions' => [
					'delete' => ['POST'],
				],
			],
		];
	}

	/**
	 * Lists all Product models.
	 * @return mixed
	 */
	public function actionIndex($description = null) {
		$searchModel = new ProductSearcher();
		$dataProvider = $searchModel->search(Yii::$app->request->queryParams, $description);
		return $this->render('index', [
					'searchModel' => $searchModel,
					'dataProvider' => $dataProvider,
		]);
	}

	public function actionSearch($searchString = null, $categoryId = null) {
		$post = \Yii::$app->request->post();
		if ($post) {
			$description = $post['searchString'];
		}
		$query = "";
		$description = $searchString;
		if (isset($description)) {
			$query = Product::find()->where(['like', 'description', $description]);
		} else {
			if (isset($categoryId)) {
				$query = Product::find()->where('categoryId=:categoryId', [':categoryId' => $categoryId]);
			} else {
				$query = Product::find();
			}
		}
		$count = $query->count();
		$pagination = new Pagination(['totalCount' => $count, 'pageSize' => '9']);
		$searchList = $query->offset($pagination->offset)->limit($pagination->limit)->orderBy('saleprice desc')->all();
		return $this->render('search', [
					'pagination' => $pagination,
					'searchList' => $searchList,
					'count' => $count,
		]);
	}

	/**
	 * Displays a single Product model.
	 * @param string $id
	 * @return mixed
	 */
	public function actionView($id) {
		$this->layout = "layout2";
		$model = $this->findModel($id);
		if (\Yii::$app->user->isGuest) {
			
		} else {
			$recentViewModel = new RecentView();
			$recentViewProductList = $recentViewModel->getRecentViewProductList();
			$recentViewModel->addRecentView($model->productId);
			return $this->render('view', [
						'model' => $model,
						'recentViewProductList' => $recentViewProductList,
			]);
		}
		return $this->render('view', [
					'model' => $model,
		]);
	}

	/**
	 * Creates a new Product model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 * @return mixed
	 */
	public function actionCreate() {
		$model = new Product();

		if ($model->load(Yii::$app->request->post()) && $model->save()) {
			return $this->redirect(['view', 'id' => $model->productId]);
		} else {
			return $this->render('create', [
						'model' => $model,
			]);
		}
	}

	/**
	 * Updates an existing Product model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param string $id
	 * @return mixed
	 */
	public function actionUpdate($id) {
		$model = $this->findModel($id);

		if ($model->load(Yii::$app->request->post()) && $model->save()) {
			return $this->redirect(['view', 'id' => $model->productId]);
		} else {
			return $this->render('update', [
						'model' => $model,
			]);
		}
	}

	/**
	 * Deletes an existing Product model.
	 * If deletion is successful, the browser will be redirected to the 'index' page.
	 * @param string $id
	 * @return mixed
	 */
	public function actionDelete($id) {
		$this->findModel($id)->delete();

		return $this->redirect(['index']);
	}

	/**
	 * Finds the Product model based on its primary key value.
	 * If the model is not found, a 404 HTTP exception will be thrown.
	 * @param string $id
	 * @return Product the loaded model
	 * @throws NotFoundHttpException if the model cannot be found
	 */
	protected function findModel($id) {
		if (($model = Product::findOne($id)) !== null) {
			return $model;
		} else {
			throw new NotFoundHttpException('The requested page does not exist.');
		}
	}

}
